package com.xaicode.template.batch.job.normal;

import com.xaicode.template.batch.decider.MyDecider;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author beborn
 * @date 2021/12/31 0031 11:08
 */
@Component
public class DeciderJobDemo {

    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Resource
    private MyDecider myDecider;

    @Bean
    public Job deciderJob() {
        return jobBuilderFactory.get("deciderJob")
                .start(step1())
                .next(myDecider)
                .from(myDecider).on(MyDecider.WEEKEND).to(step2())
                .from(myDecider).on(MyDecider.WORKING_DAY).to(step3())
                .from(step3()).on("*").to(step4())
                .end().build();
    }

    private Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet(JobHelper.tasklet("DeciderJob - 1", RepeatStatus.FINISHED))
                .build();
    }

    private Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet(JobHelper.tasklet("DeciderJob - 2", RepeatStatus.FINISHED))
                .build();
    }

    private Step step3() {
        return stepBuilderFactory.get("step3")
                .tasklet(JobHelper.tasklet("DeciderJob - 3", RepeatStatus.FINISHED))
                .build();
    }

    private Step step4() {
        return stepBuilderFactory.get("step4")
                .tasklet(JobHelper.tasklet("DeciderJob - 4", RepeatStatus.FINISHED))
                .build();
    }

}
